import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/mall'
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/Login.vue')
    },
    // 商城路由
    {
      path: '/mall',
      component: () => import('../views/mall/Layout.vue'),
      children: [
        {
          path: '',
          name: 'Products',
          component: () => import('../views/mall/Products.vue')
        },
        {
          path: 'product/:id',
          name: 'ProductDetail',
          component: () => import('../views/mall/ProductDetail.vue')
        },
        {
          path: 'cart',
          name: 'Cart',
          component: () => import('../views/mall/Cart.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: 'orders',
          name: 'Orders',
          component: () => import('../views/mall/Orders.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: 'coupons',
          name: 'Coupons',
          component: () => import('../views/mall/Coupons.vue')
        },
        {
          path: 'promotions',
          name: 'Promotions',
          component: () => import('../views/mall/Promotions.vue')
        }
      ]
    },
    // 管理后台路由
    {
      path: '/admin',
      component: () => import('../views/admin/Layout.vue'),
      meta: { requiresAuth: true, requiresAdmin: true },
      children: [
        {
          path: '',
          name: 'Dashboard',
          component: () => import('../views/admin/Dashboard.vue')
        },
        {
          path: 'products',
          name: 'ProductManagement',
          component: () => import('../views/admin/ProductManagement.vue')
        },
        {
          path: 'orders',
          name: 'OrderManagement',
          component: () => import('../views/admin/OrderManagement.vue')
        },
        {
          path: 'inventory',
          name: 'Inventory',
          component: () => import('../views/admin/Inventory.vue')
        },
        {
          path: 'delivery',
          name: 'Delivery',
          component: () => import('../views/admin/Delivery.vue')
        },
        {
          path: 'users',
          name: 'UserManagement',
          component: () => import('../views/admin/UserManagement.vue')
        },
        {
          path: 'coupons',
          name: 'CouponManagement',
          component: () => import('../views/admin/CouponManagement.vue')
        },
        // {
        //   path: 'promotions',
        //   name: 'PromotionManagement',
        //   component: () => import('../views/admin/PromotionManagement.vue')
        // }
      ]
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 检查是否需要认证
  if (to.meta.requiresAuth && !userStore.isAuthenticated) {
    next('/login')
    return
  }

  // 检查是否需要管理员权限
  if (to.meta.requiresAdmin && !userStore.isAdmin) {
    next('/mall')
    return
  }

  next()
})

export default router
